# -*- coding: utf-8 -*-
# @Time    : 2022/10/8 21:35
# @Author  : 修远
# @File    : app.py
# @Comment : 
from flask import Flask
from flask_login import LoginManager
from flask_redis import FlaskRedis
from app.config.setting import config
from app.config import secure
from app.libs.sqlalchemy import SQLAlchemy
from app.libs.customjsonencoder import CustomJSONEncoder
from flask_celery import Celery


# 注册蓝图
def register_blueprint(app):
    from app.api.v1 import create_blueprint_v1

    app.register_blueprint(create_blueprint_v1(), url_prefix='/api')


# 注册数据库
db = SQLAlchemy()
login_manager = LoginManager()
redis_client = FlaskRedis()
celery = Celery()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])  # 配置文件
    app.config.from_object(secure)
    register_blueprint(app)  # 注册蓝图
    db.init_app(app)  # 注册数据库
    login_manager.init_app(app)  # 注册登录验证
    redis_client.init_app(app)  # 注册redis
    # 对celery进行初始化操作，可以将celery的配置写在app的配置中
    celery.init_app(app=app)
    #  替换默认的json编码器
    app.json_encoder = CustomJSONEncoder
    return app
